<?php
class Report extends MY_Controller {
	var $currentPage = 'report';
	var $caption = 'Reports';
	var $action = '';
		
	public function index() {
		
		$datefrom = $this->input->get("datefrom",TRUE);
		$dateto = $this->input->get("dateto",TRUE);
		$modeOfPayment = $this->input->get('modeofpayment',TRUE);
		
		$where = array();
		$date = array();
		
		if(strlen($modeOfPayment) > 0) {
			$where = array("ordered_deal.modeofpayment" => $modeOfPayment);
		}
		
		if(strlen($datefrom) > 0 && strlen($dateto) > 0)
			$date = array($datefrom,$dateto);
		else {
			$today = new DateTime();
			$datefrom = $today->format('d/m/Y');
			$interval = new DateInterval('P1D');
			$today->add($interval);
			$dateto = $today->format('d/m/Y');
			$date = array($datefrom,$dateto);
		}
		
		$data['orderList'] = $this->mOrderedDeal->getAllOrderedDeals($where,$date)->result_object();
		$data['datetoday'] = date('m/d/Y');
		//var_dump($this->mOrderedDeal->getAllOrderedDeals($where,$date)->result_object());
		//return;
		$this->loadAdminPage($data);
	}
	
	public function exporttopdf() {
		$this->load->library('pdf/pdf');

		$this->pdf->SetCreator(PDF_CREATOR);
		$this->pdf->SetAuthor('MC Champ');
		$this->pdf->SetTitle('Ordered Deal Report');
		$this->pdf->SetSubject('Ordered Deal Report');
		//$this->pdf->SetKeywords('TCPDF, PDF, example, test, guide');
		
		// set default header data
		//$this->pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
		
		// set header and footer fonts
		//$this->pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
		//$this->pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
		
		// set default monospaced font
		$this->pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

		//set margins
		$this->pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
		$this->pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
		$this->pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
		
		//set auto page breaks
		$this->pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
		
		//set image scale factor
		//$this->pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
		
		//set some language-dependent strings
		//$this->pdf->setLanguageArray($l);

		//set header
		$this->pdf->SetTextColor(0);
        $this->pdf->SetFont('helvetica', 'B', 20);
        $this->pdf->Cell(0, 15, 'Ordered Deal Report', 0, false, 'C', 0, '', 0, false, 'M', 'M');
		
		// add a page
		$this->pdf->AddPage();		
		
		//set header of table
		$header = array("Tenant","Item","Client","Mode of Payment","Recipient","Email","Quantity","Total Amount");		

		$datefrom = $this->input->get("datefrom",TRUE);
		$dateto = $this->input->get("dateto",TRUE);
		$modeOfPayment = $this->input->get('modeofpayment',TRUE);
		
		$where = array();
		$date = array();
		
		if(strlen($modeOfPayment) > 0) {
			$where = array("modeofpayment" => $modeOfPayment);
		}
		
		if(strlen($datefrom) > 0 && strlen($dateto) > 0)
			$date = array($datefrom,$dateto);
		else {
			$today = new DateTime();
			$datefrom = $today->format('m/d/Y');
			$interval = new DateInterval('P1D');
			$today->add($interval);
			$dateto = $today->format('m/d/Y');
			$date = array($datefrom,$dateto);
		}		
		
		$this->pdf->SetFont('helvetica', '', 11);
		$dateRange = "Date : $datefrom -- $dateto";
        $this->pdf->Cell(0, 10, $dateRange, 0, 0, 'L');
		$this->pdf->ln();
		
		//set data 
		$orderList = $this->mOrderedDeal->getAllOrderedDeals($where,$date)->result_array();
		
		//var_dump($orderList);
		//return;
		//set some text to print
		$this->buildTable($header,$orderList);
		
		$this->pdf->Output('ordered_deal_report.pdf', 'I');		
	}
	
	/**
	 * 
	 * tabular formatting for pdf
	 * @param Array $header 
	 * @param Array $data
	 */
	private function buildTable($header, $data) {
		
		//set width of columns
		$width = array("35","40","35","40","35","35","25","30");
		
		$this->pdf->SetFillColor(255,255,255);
		$this->pdf->SetTextColor(0);
		$this->pdf->SetDrawColor(128,0,0);
		$this->pdf->SetLineWidth(.3);
		$this->pdf->SetFont('','B',13);
		
		for($i=0; $i<count($header); $i++) {
			$this->pdf->Cell($width[$i],7,$header[$i],1,0,'C',1);
		}
		$this->pdf->ln();
		
		$this->pdf->SetFillColor(175);
		$this->pdf->SetTextColor(0);
		$this->pdf->SetDrawColor(128,0,0);
		$this->pdf->SetLineWidth(.3);
		$this->pdf->SetFont('');
		$fill = true;
		
		foreach($data as $row) {
			
			$this->pdf->Cell($width[0],6,$row['tenant_name'],"LR",0,"L",$fill);
			
			$this->pdf->Cell($width[1],6,$row['item_name'],"LR",0,"L",$fill);

			$this->pdf->Cell($width[2],6,$row['firstname'],"LR",0,"L",$fill);
			
			$this->pdf->Cell($width[3],6,$row['modeofpayment'],"LR",0,"L",$fill);
			
			$this->pdf->Cell($width[4],6,$row['receipient_fullname'],"LR",0,"L",$fill);
			
			$this->pdf->Cell($width[5],6,$row['receipient_email'],"LR",0,"L",$fill);
			
			$this->pdf->Cell($width[6],6,$row['deal_item_quantity'],"LR",0,"L",$fill);
			
			$this->pdf->Cell($width[7],6,$row['total_amount'],"LR",0,"L",$fill);
			
			$this->pdf->ln();
			
			$fill =! $fill;
			
		}
		// print a block of text using Write()
		//$this->pdf->Write($h=0, $txt, $link='', $fill=0, $align='C', $ln=true, $stretch=0, $firstline=false, $firstblock=false, $maxh=0);
		
	}
	
	public function exporttoexcel() {
		$this->load->library('Excel');
		$this->load->helper('download');
		
		$this->createDirectory();
		
		$this->excel->getProperties()->setCreator('MCCHAMP');
		$this->excel->getProperties()->setLastModifiedBy('currentuser');
		$this->excel->getProperties()->setTitle('Ordered Deal Report');
		$this->excel->getProperties()->setSubject('Ordered Deal Report');
		
		$this->excel->createSheet();
		
		$this->excel->getActiveSheet()->mergeCells('A1:E1');
		$this->excel->getActiveSheet()->setCellValueByColumnAndRow(0,1,'Ordered Deal Report');
		
		$this->excel->getActiveSheet()->getDefaultColumnDimension()->setWidth(25);

		
		/** create header **/
		$columnHeader = array('Date Created', 'Item Code', 'Item', 'Tenant', 'Mode of Payment', 'Deal Type', 'Recipient', 'Recipient Email', 'Recipient Address','Client Name','Client Address', 'Quantity', 'Total Amount');
		
		for($col = 0; $col <= count($columnHeader)-1; $col++) {
			for($row = 3; $row <= 3; $row++) {
				$this->excel->getActiveSheet()->setCellValueByColumnAndRow($col,$row,$columnHeader[$col]);		
			}
		}

		$datefrom = $this->input->get("datefrom",TRUE);
		$dateto = $this->input->get("dateto",TRUE);
		$modeOfPayment = $this->input->get('modeofpayment',TRUE);
		
		$where = array();
		$date = array();

		if(strlen($modeOfPayment) > 0) {
			$where = array("modeofpayment" => $modeOfPayment);
		}
		
		$interval = new DateInterval('P1D');
		
		if(strlen($datefrom) > 0 && strlen($dateto) > 0) {
			$date = array($datefrom,$dateto);
			
		} else {
			$today = new DateTime();
			$datefrom = $today->format('m/d/Y');
			$today->add($interval);
			$dateto = $today->format('m/d/Y');
			$date = array($datefrom,$dateto);
		}
		
		$orderList = $this->mOrderedDeal->getAllOrderedDeals($where,$date)->result_array();
			
		$row = 5;
		foreach($orderList as $val) {
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(0,$row,$val['created_on']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(1,$row,$val['item_code']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(2,$row,$val['item_name']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(3,$row,$val['tenant_name']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(4,$row,$val['modeofpayment']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(5,$row,$val['dealtype']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(6,$row,$val['receipient_fullname']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(7,$row,$val['receipient_email']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(8,$row,$val['receipient_address']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(9,$row,$val['client_name']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(10,$row,$val['client_address']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(11,$row,$val['deal_item_quantity']);
			$this->excel->getActiveSheet()->setCellValueByColumnAndRow(12,$row,$val['total_amount']);
			$row++;		
		}
		
		$excelWriter = PHPExcel_IOFactory::createWriter($this->excel,'Excel5');
		$excelFilename = EXCEL_PATH . 'ordered_deal_report.xls';
		$excelWriter->save($excelFilename);
		
		force_download('ordered_deal_report.xls', file_get_contents($excelFilename));
	}
	
	private function createDirectory() {
		if(!is_dir(EXCEL_PATH))
	    	mkdir(EXCEL_PATH,DIR_WRITE_MODE,TRUE);		
	}
}
?>